package com.wlyuan.open.controller;

import com.wlyuan.basic.api.IConsigneeService;
import com.wlyuan.basic.dto.consignee.ConsigneeDetailDTO;
import com.wlyuan.basic.dto.consignee.ConsigneeFilterDTO;
import com.wlyuan.core.marker.NamedStatus;
import com.wlyuan.open.annotation.OpenApi;
import com.wlyuan.open.assembler.ConsigneeAssembler;
import com.wlyuan.open.basic.dto.OpenConsigneeCreationDTO;
import com.wlyuan.open.basic.dto.OpenConsigneeUpdateDTO;
import com.wlyuan.utils.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * @author lijing
 */
@Api(tags = "收货方")
@Validated
@RequestMapping("/api/consignee")
@RestController
public class OpenConsigneeController {

    @DubboReference(check = false)
    private IConsigneeService consigneeService;

    @ApiOperation("创建收货方")
    @PostMapping("/")
    @OpenApi
    public Long create(@RequestBody @Valid OpenConsigneeCreationDTO openConsignee) {
        var consignee = ConsigneeAssembler.assemble(openConsignee);
        var detail = consigneeService.create(consignee);
        return detail.getConsigneeId();
    }

    @ApiOperation("修改收货方")
    @PutMapping("/")
    @OpenApi
    public void update(@RequestBody @Valid OpenConsigneeUpdateDTO openConsignee) {
        var consignee = ConsigneeAssembler.assemble(openConsignee);
        consigneeService.update(consignee);
    }

    //todo 查询及返回都需要自己的dto
    @ApiOperation("查询收货方信息")
    @PostMapping("/search")
    @OpenApi
    public Page<ConsigneeDetailDTO> search(@RequestBody @Valid ConsigneeFilterDTO filter) {
        filter.setNames(Arrays.asList(filter.getName()));
        filter.setName(null);
        filter.setCustomerNames(Arrays.asList(filter.getCustomerName()));
        filter.setCustomerName(null);
        filter.setStatus(NamedStatus.ENABLED.getValue());
        var consigness = consigneeService.search(filter);
        return consigness;
    }
}
